package com.fingard.dsp.bank.directbank.nnbill01.util;

import com.alibaba.fastjson.JSONObject;
import com.bill99.sdk.crypto.process.CryptoManager;
import com.bill99.sdk.crypto.process.CryptoProcessor;
import com.bill99.sdk.crypto.schema.CmsSignedAndEnveloped;

public class MerchantPKIUtils {
    /**
     * 加密
     * 
     * @param kuaiqianPublicCertFile 快钱提供的公钥证书
     * @param MerchantPrivateCertFile 商户自己生成的私钥证书
     * @param originalData 待加密字符串(UTF-8编码)
     * @return 加密后对象体
     * @throws Exception
     */
    
    public static CmsSignedAndEnveloped encrypt(String kuaiqianPublicCertFile, String MerchantPrivateCertFile, String originalData)
            throws Exception {
        CryptoProcessor processor = CryptoManager.createProcessor(kuaiqianPublicCertFile, MerchantPrivateCertFile);
        CmsSignedAndEnveloped cmsSignedAndEnveloped = processor.cmsSignAndEnvelope(originalData);
        System.out.println("PKI ENCRYPTED=" + JSONObject.toJSONString(cmsSignedAndEnveloped));
        return cmsSignedAndEnveloped;
    }
    
    /**
     * 解密
     * 
     * @param kuaiqianPublicCertFile 快钱提供的公钥证书
     * @param MerchantPrivateCertFile 商户自己生成的私钥证书
     * @param cmsSignedAndEnveloped 解密前对象体
     * @return 将解密后字符串包装成JSONObject对象体
     * @throws Exception
     */
    public static JSONObject decrypt(String kuaiqianPublicCertFile, String MerchantPrivateCertFile, CmsSignedAndEnveloped cmsSignedAndEnveloped)
            throws Exception {
    	 CryptoProcessor processor = CryptoManager.createProcessor(kuaiqianPublicCertFile, MerchantPrivateCertFile);
        String result = processor.openCmsEnvelopeAndVerify(cmsSignedAndEnveloped);
        System.out.println("PKI DECRYPTED=" + result);
        return JSONObject.parseObject(result);
    }
}
